package top.atluofu.luofuecommerce.product.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.atluofu.luofuecommerce.product.entity.CategoryEntity;
import top.atluofu.luofuecommerce.product.service.CategoryService;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: IndexController
 * @description: TODO
 * @author: 有罗敷的马同学
 * @datetime: 2022Year-11Month-16Day-20:21
 * {@code @Version:} 1.0
 */
@Controller
public class IndexController {

    final
    CategoryService categoryService;

    @Autowired
    public IndexController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping({"/", "index.html"})
    public String index(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categories();
        model.addAttribute("categories", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catelog.json")
    public Map<String, List<Object>> getCatelogJson() {
        return categoryService.getCatelogJson();
    }
}
